// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Xml;

using QuoteAdvantageEditor.CrmSdk;


namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Dialog for adding rules to parent products.
	/// </summary>
	public class ParentLinkedItemDlg : System.Windows.Forms.Form
	{
      private AuthoringEngine eng = null;
      protected XmlNode objInputRuleNode = null;
      protected XmlNode objOutputRuleNode = null;
     
      private System.Windows.Forms.Button btnOK;
      private System.Windows.Forms.Button btnCancel;
      private System.Windows.Forms.ComboBox ddlLinkedUnitOfMeasure;
      private System.Windows.Forms.Label lblLinkedUom;
      private System.Windows.Forms.Button btnSearch3;
      private System.Windows.Forms.Label lblLinkedItemId;
      private System.Windows.Forms.TextBox txtLinkedItemId;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
      
      public AuthoringEngine Engine
      {
         get { return eng; }
         set { eng = value; }
      }

      public XmlNode InputRuleNode
      {
         get { return objInputRuleNode; }
         set { objInputRuleNode = value; }
      }

      public XmlNode OutputRuleNode
      {
         get { return objOutputRuleNode; }
         set { objOutputRuleNode = value; }
      }

		public ParentLinkedItemDlg()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.btnOK = new System.Windows.Forms.Button();
         this.btnCancel = new System.Windows.Forms.Button();
         this.ddlLinkedUnitOfMeasure = new System.Windows.Forms.ComboBox();
         this.lblLinkedUom = new System.Windows.Forms.Label();
         this.btnSearch3 = new System.Windows.Forms.Button();
         this.lblLinkedItemId = new System.Windows.Forms.Label();
         this.txtLinkedItemId = new System.Windows.Forms.TextBox();
         this.SuspendLayout();
         // 
         // btnOK
         // 
         this.btnOK.Location = new System.Drawing.Point(192, 100);
         this.btnOK.Name = "btnOK";
         this.btnOK.TabIndex = 18;
         this.btnOK.Text = "OK";
         this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
         // 
         // btnCancel
         // 
         this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
         this.btnCancel.Location = new System.Drawing.Point(272, 100);
         this.btnCancel.Name = "btnCancel";
         this.btnCancel.TabIndex = 19;
         this.btnCancel.Text = "Cancel";
         this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
         // 
         // ddlLinkedUnitOfMeasure
         // 
         this.ddlLinkedUnitOfMeasure.DisplayMember = "Text";
         this.ddlLinkedUnitOfMeasure.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
         this.ddlLinkedUnitOfMeasure.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.ddlLinkedUnitOfMeasure.ItemHeight = 13;
         this.ddlLinkedUnitOfMeasure.Location = new System.Drawing.Point(104, 56);
         this.ddlLinkedUnitOfMeasure.Name = "ddlLinkedUnitOfMeasure";
         this.ddlLinkedUnitOfMeasure.Size = new System.Drawing.Size(156, 21);
         this.ddlLinkedUnitOfMeasure.TabIndex = 15;
         this.ddlLinkedUnitOfMeasure.ValueMember = "Value";
         // 
         // lblLinkedUom
         // 
         this.lblLinkedUom.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.lblLinkedUom.Location = new System.Drawing.Point(12, 60);
         this.lblLinkedUom.Name = "lblLinkedUom";
         this.lblLinkedUom.Size = new System.Drawing.Size(92, 16);
         this.lblLinkedUom.TabIndex = 14;
         this.lblLinkedUom.Text = "Unit of Measure:";
         this.lblLinkedUom.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
         // 
         // btnSearch3
         // 
         this.btnSearch3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.btnSearch3.Location = new System.Drawing.Point(264, 16);
         this.btnSearch3.Name = "btnSearch3";
         this.btnSearch3.Size = new System.Drawing.Size(72, 24);
         this.btnSearch3.TabIndex = 13;
         this.btnSearch3.Text = "Search";
         this.btnSearch3.Click += new System.EventHandler(this.btnSearch3_Click);
         // 
         // lblLinkedItemId
         // 
         this.lblLinkedItemId.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.lblLinkedItemId.Location = new System.Drawing.Point(28, 20);
         this.lblLinkedItemId.Name = "lblLinkedItemId";
         this.lblLinkedItemId.Size = new System.Drawing.Size(72, 16);
         this.lblLinkedItemId.TabIndex = 11;
         this.lblLinkedItemId.Text = "Item ID:";
         this.lblLinkedItemId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
         // 
         // txtLinkedItemId
         // 
         this.txtLinkedItemId.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.txtLinkedItemId.Location = new System.Drawing.Point(104, 16);
         this.txtLinkedItemId.Name = "txtLinkedItemId";
         this.txtLinkedItemId.Size = new System.Drawing.Size(152, 21);
         this.txtLinkedItemId.TabIndex = 12;
         this.txtLinkedItemId.Text = "";
         this.txtLinkedItemId.TextChanged += new System.EventHandler(this.txtLinkedItemId_TextChanged);
         // 
         // ParentLinkedItemDlg
         // 
         this.AcceptButton = this.btnOK;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
         this.ClientSize = new System.Drawing.Size(358, 136);
         this.Controls.Add(this.btnCancel);
         this.Controls.Add(this.btnOK);
         this.Controls.Add(this.lblLinkedItemId);
         this.Controls.Add(this.txtLinkedItemId);
         this.Controls.Add(this.ddlLinkedUnitOfMeasure);
         this.Controls.Add(this.btnSearch3);
         this.Controls.Add(this.lblLinkedUom);
         this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "ParentLinkedItemDlg";
         this.ShowInTaskbar = false;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Linked Item";
         this.Load += new System.EventHandler(this.RuleDlg_Load);
         this.ResumeLayout(false);

      }
		#endregion
      
      private void RuleDlg_Load(object sender, System.EventArgs e)
      {
         SetControlValues();
      }

      private void btnOK_Click(object sender, System.EventArgs e)
      {
         GetControlValues();
         this.DialogResult = DialogResult.OK;
         this.Close();
      }

      private void btnCancel_Click(object sender, System.EventArgs e)
      {
         this.Close();
      }           

      private void SetControlValues()
      {
         txtLinkedItemId.Text = objInputRuleNode.InnerText;   
         RefreshLinkItemUom();
      }

      private void GetControlValues()
      {
            objOutputRuleNode = objInputRuleNode.OwnerDocument.CreateElement("", "LinkedItem", "");
            objOutputRuleNode.InnerText = txtLinkedItemId.Text;
            XmlAttribute objAttrib = objInputRuleNode.OwnerDocument.CreateAttribute("", "uom", "");

            objAttrib.Value = ""; 
            objOutputRuleNode.Attributes.Append(objAttrib);

            string strUomValue = "";

            if (ddlLinkedUnitOfMeasure.SelectedIndex != -1)
            {
               ListItem objItem = (ListItem)ddlLinkedUnitOfMeasure.SelectedItem;
               strUomValue = objItem.Value;
            }

         objOutputRuleNode.Attributes["uom"].Value = strUomValue;      
      }

      private void RefreshLinkItemUom()
      {
         string strDefaultUom = "";
         
         if (objInputRuleNode.Attributes["uom"] != null)
         {
            strDefaultUom = objInputRuleNode.Attributes["uom"].Value;               
         }
         
         Product objProduct = eng.LoadProductFromProductNumber(txtLinkedItemId.Text);

         ddlLinkedUnitOfMeasure.Items.Clear();
         
         if (objProduct != null)
         {              
            ArrayList objUomList = eng.LoadUnitOfMeasureList(objProduct);

            ddlLinkedUnitOfMeasure.Items.Clear();
            ddlLinkedUnitOfMeasure.Items.Add(new ListItem("[Default]", ""));  
            ddlLinkedUnitOfMeasure.SelectedIndex = 0;

            foreach(uom objUnit in objUomList)
            {
               string strUom = objUnit.uomid.Value.ToString();
               ddlLinkedUnitOfMeasure.Items.Add(new ListItem(objUnit.name, strUom));  
                
               if (strUom == strDefaultUom)
               {
                  ddlLinkedUnitOfMeasure.SelectedIndex = ddlLinkedUnitOfMeasure.Items.Count - 1;
               }
            }
         }      
      }

      private void btnSearch3_Click(object sender, System.EventArgs e)
      {
         LoadCrmProductDlg dlg = new LoadCrmProductDlg();

         dlg.Engine = this.Engine;

         if (DialogResult.OK == dlg.ShowDialog(this))
         {
            txtLinkedItemId.Text = dlg.SelectedProduct.ProductNumber;
            RefreshLinkItemUom();
         }
      }

      private void txtLinkedItemId_TextChanged(object sender, System.EventArgs e)
      {
         RefreshLinkItemUom();
      }



	} // end class
}
